// src/services/api.ts
import axios from 'axios';

const API_BASE_URL = '/api';

class ApiService {
  async getContents(page: number = 0, size: number = 20, tagId?: number) {
    const params = new URLSearchParams();
    params.append('page', page.toString());
    params.append('size', size.toString());
    if (tagId) params.append('tagId', tagId.toString());
    
    const response = await axios.get(`${API_BASE_URL}/contents`, { params });
    return response.data;
  }

  async getContentById(id: number) {
    const response = await axios.get(`${API_BASE_URL}/contents/${id}`);
    return response.data;
  }

  async rateContent(contentId: number, rating: number) {
    const response = await axios.post(`${API_BASE_URL}/contents/${contentId}/rate`, { rating });
    return response.data;
  }

  async toggleFavorite(contentId: number) {
    const response = await axios.post(`${API_BASE_URL}/contents/${contentId}/favorite`);
    return response.data;
  }

  async removeFavorite(contentId: number) {
    const response = await axios.delete(`${API_BASE_URL}/contents/${contentId}/favorite`);
    return response.data;
  }

  async getTags() {
    const response = await axios.get(`${API_BASE_URL}/tags`);
    return response.data;
  }

  async createTag(name: string) {
    const response = await axios.post(`${API_BASE_URL}/tags`, { name });
    return response.data;
  }

  async addTagToContent(contentId: number, tagId: number) {
    const response = await axios.post(`${API_BASE_URL}/contents/${contentId}/tags`, { tagId });
    return response.data;
  }

  async removeTagFromContent(contentId: number, tagId: number) {
    const response = await axios.delete(`${API_BASE_URL}/contents/${contentId}/tags/${tagId}`);
    return response.data;
  }

  async getUserFavorites() {
    const response = await axios.get(`${API_BASE_URL}/user/favorites`);
    return response.data;
  }

  async getUserRatings() {
    const response = await axios.get(`${API_BASE_URL}/user/ratings`);
    return response.data;
  }

  async getUserHistory() {
    const response = await axios.get(`${API_BASE_URL}/user/history`);
    return response.data;
  }
}

export default new ApiService();